package cn.kgc.springboot.mybatis.service.impl;

import cn.kgc.springboot.mybatis.entity.User;
import cn.kgc.springboot.mybatis.mapper.UserMapper;
import cn.kgc.springboot.mybatis.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/24
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> getList() {
        return userMapper.getList();
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public Map deleteById(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            userMapper.deleteById(id);
            map.put("code",0);
            map.put("msg","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }

    @Override
    public Map updateUser(User user) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            userMapper.updateUser(user);
            map.put("code",0);
            map.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","修改失败");
        }
        return map;
    }

    @Override
    public Map addUser(User user) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            userMapper.addUser(user);
            map.put("code",0);
            map.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","添加失败");
        }
        return map;
    }

    @Override
    public List<User> getPage(Integer page, Integer limit) {

        PageHelper.startPage(page,limit);
        List<User> list = userMapper.getList();
        PageInfo<User> userPageInfo = new PageInfo<>(list);
        System.out.println("userPageInfo = " + userPageInfo);
        return list;
    }

}

